package com.lonwin.inspectionmanagement.mvp.ui.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.http.imageloader.ImageConfig;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ImageBean;
import com.lonwin.inspectionmanagement.mvp.ui.activity.CreatPointRecordActivity;

import java.util.List;

/**
 * Author: zhangjing
 * DATA:2018/11/23 下午8:34
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class OnePicAdapter extends BaseQuickAdapter<ImageBean,BaseViewHolder>{
    ImageLoader mImageLoader;
    private CreatPointRecordActivity.Type mType=CreatPointRecordActivity.Type.ADD;

    public void setType(CreatPointRecordActivity.Type type) {
        mType = type;
    }

    public OnePicAdapter(@Nullable List<ImageBean> data) {
        super(R.layout.item_image, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ImageBean item) {
        if (mImageLoader==null){
            mImageLoader= ArmsUtils.obtainAppComponentFromContext(mContext).imageLoader();
        }
        ImageView imageView=helper.getView(R.id.img);
        ImageView closeview=helper.getView(R.id.iv_close);
        if (!TextUtils.isEmpty(item.getUrl())){
            if (mType==CreatPointRecordActivity.Type.PUB||mType==CreatPointRecordActivity.Type.HANDOVER){
                closeview.setVisibility(View.GONE);
            }else {
                closeview.setVisibility(View.VISIBLE);
                closeview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setPath(null);
                        item.setUrl(null);
                        item.setId(null);
                        notifyDataSetChanged();
                    }
                });
            }
            Glide.with(mContext).load(item.getUrl()).into(imageView);
//            mImageLoader.loadImage(mContext, ImageConfigImpl.builder().url(item.getUrl()).imageView(imageView).build());
        }else {
            closeview.setVisibility(View.GONE);
            Glide.with(mContext).load(R.drawable.app_photo_icon).into(imageView);
        }
    }
}
